﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.PlcConnectPoolDomain;
using Zocono.WCS.Domain.ProcessModuleDomain;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;
using Zocono.WCS.Infrastructure.PlcCommon;
using Zocono.WCS.WMSApiDomain.WebApi;

namespace Zocono.WCS.Domain.EquipmentProcessDomain.PlanarGeneralMessage
{
    /// <summary>
    /// 巷道位置请求
    /// </summary>
    public class AisleLocationRequestService : IPlanarGeneralPlcActionProcessService
    {
        private readonly ISnowflake _snowflake;
        private readonly ILogUtil _logUtil;
        private readonly IMapper _mapper;
        private readonly IPlcConnectionPool _plcConnectionPool;
        private readonly IWorkTaskInfoRepository _workTaskInfoRepository;
        private readonly IPlcWriteDbInfoRepository _plcWriteDbInfoRepository;
        private readonly ITaskRouteDetailRecordInfoRepository _taskRouteDetailRecordInfoRepository;
        private readonly IRouteDetailInfoRepository _routeDetailInfoRepository;
        private readonly IRouteInfoRepository _routeInfoRepository;
        private readonly IPlcConnectionStateInfoRepository _IPlcConnectionStateInfoRepository;

        public AisleLocationRequestService(ISnowflake snowflake, ILogUtil logUtil, IMapper mapper,
                IPlcConnectionPool plcConnectionPool, IWorkTaskInfoRepository workTaskInfoRepository, IPlcWriteDbInfoRepository plcWriteDbInfoRepository
                , ITaskRouteDetailRecordInfoRepository taskRouteDetailRecordInfoRepository, IRouteDetailInfoRepository routeDetailInfoRepository
                , IRouteInfoRepository routeInfoRepository, IPlcConnectionStateInfoRepository plcConnectionStateInfoRepository)
        {
            _snowflake = snowflake;
            _logUtil = logUtil;
            _mapper = mapper;
            _plcConnectionPool = plcConnectionPool;
            _workTaskInfoRepository = workTaskInfoRepository;
            _plcWriteDbInfoRepository = plcWriteDbInfoRepository;
            _taskRouteDetailRecordInfoRepository = taskRouteDetailRecordInfoRepository;
            _routeDetailInfoRepository = routeDetailInfoRepository;
            _routeInfoRepository = routeInfoRepository;
            _IPlcConnectionStateInfoRepository = plcConnectionStateInfoRepository;
        }

        public async Task ProcessActionMessageAsync(BytesObjectBase plcActionMessage, BytesObjectBase wcsActionMessage, EquipmentDbObject planarEquipmentDbObject/*, BytesObjectBase plcStateMassge*/)
        {
            /*
                1. Plc离线判断&设备报警判断
                2. plc写入连接获取&写入报文构建
                3. 托盘号为空判断
                4. wcs任务存在执行中且已分配货位逻辑处理 => Return
                5. wcs不存在任务逻辑处理 => Return
             */
            var plcMessage = plcActionMessage as PlanarGeneralPlcActionBO;//Plc的动作报文 没有任务号 目标位置（默认为自己）
            var wcsMessage = wcsActionMessage as PlanarGeneralWcsActionBO;//Wcs的动作报文
            //var plcStateBo = plcStateMassge as PlcOnLineStateBO;//Plc在线离线报文 没有设备信息
            PlanarGeneralWriteWcsAction writeBo = null;//写入Plc的报文
            PlcConnectionS7 plcConn = _plcConnectionPool.S7GetWritePlcConnection(plcMessage.PlcConnectionID);//plc写入连接（不锁定）

            _logUtil.Info($"AisleRequestService接收报文  PLCId:{plcMessage.PlcConnectionID} DBId:{plcMessage.PlcReadDbID}  设备编号{plcMessage.EquipmentCode},PLC握手{plcMessage.ActionType} WCS握手{wcsMessage.ActionType}");


            #region Plc离线判断&设备报警判断
            var plcIPStates = await _IPlcConnectionStateInfoRepository.GetFirstAsync(l => l.PlcConnectionID == plcMessage.PlcConnectionID && l.Connected == true);
            if (plcIPStates == null)
            {
                _logUtil.Info($"请求巷道分配失败：设备编号{plcMessage.EquipmentCode}{plcConn.IP}plc已离线");
                return;
            }
            //plc在线离线判断
            //if (plcStateBo.PlcState == 0)
            //{
            //    _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}{plcConn.IP}plc已离线");
            //    return;
            //}

            //设备报警状态判断
            //var stateMessage = planarEquipmentDbObject.bytesObjectBases.Find(o => o.ObjectType.Contains("StateBO"));
            //var planarStateBo = stateMessage as PlanarStateBO;
            //if (planarStateBo == null)
            //{
            //    _logUtil.Info($"巷道&货位请求失败：设备编号{plcMessage.EquipmentCode} 状态信息不存在");
            //    return;
            //}

            //if (planarStateBo.AlarmCode != 0)
            //{
            //    _logUtil.Info($"巷道&货位请求失败：设备编号{plcMessage.EquipmentCode}报警,报警代码：{planarStateBo.AlarmCode}");
            //    return;
            //}
            #endregion

            #region plc写入连接获取&写入报文构建
            //Plc连接Id
            long plcId = wcsMessage.PlcConnectionID;
            //写入plc的报文配置获取
            var objectType = typeof(PlanarGeneralWriteWcsAction).Name;
            //向数据库配置好的DB块获取点位
            var writeConfig = await _plcWriteDbInfoRepository.GetFirstAsync(s => s.EquipmentCode ==
            plcMessage.EquipmentCode.ToString() && s.ObjectType == objectType);
            if (writeConfig == null)
            {
                _logUtil.Info($"巷道&货位请求失败：设备编号{plcMessage.EquipmentCode},获取PLC写入配置失败");
                return;
            }

            //构建写入报文
            writeBo = new PlanarGeneralWriteWcsAction(
                writeConfig.PlcConnectionId,
                writeConfig.ID,
                writeConfig.DbCode,
                writeConfig.DbStart,
                objectType,
                Convert.ToInt32(writeConfig.EquipmentCode),
                plcMessage.ActionType,
                toEquipmentCode: Convert.ToInt32(plcMessage.EquipmentCode),
                taskCode: plcMessage.TaskCode,
                palletBarcode: plcMessage.PalletBarcode,
                plcMessage.PalletType,
                alrmCode: plcMessage.AlrmCode);

            if (plcConn == null)
            {
                _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}获取连接({writeBo.PlcConnectionID})失败");
                return;
            }
            #endregion

            #region 托盘号为空判断
            //托盘号不存在 写入握手类型100报警
            if (string.IsNullOrEmpty(plcMessage.PalletBarcode))
            {
                _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}托盘信息不存在！");
                writeBo.ActionType = 100;
                var writeBl = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());
                if (writeBl == false)
                {
                    _logUtil.Info($"巷道&货位请求失败：PLC(IP{plcConn.IP},设备编号{writeBo.EquipmentCode})写入报文失败");
                }
                else
                {
                    _logUtil.Info($"AisleRequestService发送报文  PLCId:{writeBo.PlcConnectionID} DBId:{writeBo.PlcWriteDbID}  设备编号{writeBo.EquipmentCode} WCS握手{writeBo.ActionType}");

                }
                return;
            }

            if (plcMessage.PalletBarcode.Length != 16)
            {
                _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}托盘条码长度不是16位！");
                return;
            }

            #endregion

            #region wcs任务存在执行中且已分配货位逻辑处理
            //wcs获取执行中已分配货位的任务信息
            var wcsWorkTaskInfo = await _workTaskInfoRepository.GetFirstAsync(s => s.PalletCode == writeBo.PalletBarcode
                && s.TaskState == EnumTaskState.Executing
                && (s.TaskType == EnumTaskType.STA));

            if (wcsWorkTaskInfo != null)
            {
                //分配上架货位
                if (wcsWorkTaskInfo.TaskProcessState == EnumTaskProcessState.Initial)//任务为初始状态
                {
                    //申请货位
                    //获取下一子任务
                    var nextRouteDetailInfo = await _routeDetailInfoRepository.GetFirstAsync(s => s.PrevionsID == wcsWorkTaskInfo.CurrentRounteDetailID);
                    if (nextRouteDetailInfo == null)
                    {
                        _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}巷道路径详情配置信息起始工位{wcsWorkTaskInfo.WmsFromStation}目标工位{wcsWorkTaskInfo.WmsToStation}获取失败");
                        return;
                    }

                    //wcs请求货位
                    AllotLocationOutputDto allotLocationOutputDto = null;
                    if (nextRouteDetailInfo.DispatchType == EnumDispatchType.SC_UP)
                    {
                        try
                        {

                            //巷道详情信息获取
                            var routeDetailConfigInfo = await _routeDetailInfoRepository.GetFirstAsync(s => s.RouteID == wcsWorkTaskInfo.RouteID && s.IsStart);
                            //int colum = !string.IsNullOrEmpty(routeDetailConfigInfo.FromCargo) ? Convert.ToInt32(routeDetailConfigInfo.FromCargo.Substring(4, 3)) : 1;
                            WMSApiDomain.WebApi.HeaderResult<AllotLocationOutputDto> res = await WCSMessageWithWMSApi.WCSGetAllotLocatonAsync(wcsWorkTaskInfo.WmsTaskCode, plcMessage.EquipmentCode.ToString(),
                                plcMessage.PalletBarcode, routeDetailConfigInfo.FromCargoColumn);//向WMS获取货位信息
                            if (res.IsSucceed)
                            {
                                allotLocationOutputDto = res.Result;
                            }
                            else
                            {
                                //runLogInfo.RunLog = $"向Wms请求货位失败:{res.Message}";
                                //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                                return;
                            }
                        }
                        catch (Exception ex)
                        {
                            _logUtil.Error($"巷道&货位请求失败：Wms请求货位失败，托盘号{plcMessage.PalletBarcode}，设备编号{plcMessage.EquipmentCode}！异常：{ex.ToString()}");
                            return;
                        }
                    }

                    //装载位子任务完成 保存子任务记录
                    TaskRouteDetailRecordInfo naTaskRouteDetailRecordInfo = new TaskRouteDetailRecordInfo();
                    naTaskRouteDetailRecordInfo.ID = _snowflake.GetId();
                    naTaskRouteDetailRecordInfo.TaskID = wcsWorkTaskInfo.ID;
                    naTaskRouteDetailRecordInfo.RouteDetailID = wcsWorkTaskInfo.CurrentRounteDetailID;
                    naTaskRouteDetailRecordInfo.DetailTaskState = EnumDetailTaskState.Finish;
                    naTaskRouteDetailRecordInfo.DispatchType = wcsWorkTaskInfo.CurrentDispatchType;
                    naTaskRouteDetailRecordInfo.FromEquipment = wcsWorkTaskInfo.CurrentFromEquipment;
                    naTaskRouteDetailRecordInfo.FromCargo = wcsWorkTaskInfo.CurrentFromCargo;
                    naTaskRouteDetailRecordInfo.ToEquipment = wcsWorkTaskInfo.CurrentToEquipment;
                    naTaskRouteDetailRecordInfo.ToCargo = wcsWorkTaskInfo.CurrentToCargo;
                    naTaskRouteDetailRecordInfo.CreateTime = DateTime.Now;
                    naTaskRouteDetailRecordInfo.UpdateTime = DateTime.Now;

                    if (allotLocationOutputDto != null)
                    {
                        wcsWorkTaskInfo.WmsToPosition = allotLocationOutputDto.LocationCode;
                    }
                    //更新当前子任务
                    wcsWorkTaskInfo.CurrentRounteDetailID = nextRouteDetailInfo.ID;
                    wcsWorkTaskInfo.CurrentDetailTaskState = EnumDetailTaskState.Init;
                    wcsWorkTaskInfo.CurrentDispatchType = nextRouteDetailInfo.DispatchType;
                    wcsWorkTaskInfo.CurrentFromEquipment = nextRouteDetailInfo.FromEquipment;
                    wcsWorkTaskInfo.CurrentFromCargo = nextRouteDetailInfo.FromCargo;
                    wcsWorkTaskInfo.CurrentToEquipment = nextRouteDetailInfo.ToEquipment;
                    wcsWorkTaskInfo.CurrentToCargo = nextRouteDetailInfo.ToCargo == "ToCargo" ? allotLocationOutputDto.LocationCode : nextRouteDetailInfo.ToCargo;
                    wcsWorkTaskInfo.CurrentUpdateTime = DateTime.Now;
                    wcsWorkTaskInfo.TaskProcessState = EnumTaskProcessState.AllotLocation;

                    //更新数据库
                    var result2 = await _taskRouteDetailRecordInfoRepository.AsTenant().UseTranAsync(async () =>
                    {
                        await _taskRouteDetailRecordInfoRepository.InsertAsync(naTaskRouteDetailRecordInfo);
                        await _workTaskInfoRepository.UpdateAsync(wcsWorkTaskInfo);
                    });
                }

                //存在回复正常报文
                writeBo.TaskCode = wcsWorkTaskInfo.TaskCode;
                var writeBl = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());

                if (!writeBl)
                {
                    _logUtil.Info($"巷道&货位请求失败：PLC(IP{plcConn.IP},设备编号{writeBo.EquipmentCode})写入报文失败");
                    return;
                }
                else
                {
                    _logUtil.Info($"AisleRequestService发送报文  PLCId:{writeBo.PlcConnectionID} DBId:{writeBo.PlcWriteDbID}  设备编号{writeBo.EquipmentCode} WCS握手{writeBo.ActionType}");
                    return;
                }
            }
            #endregion

            #region wcs不存在任务逻辑处理
            //一阶段wms请求入库任务
            WmsTaskInfoDto workTaskDto = null;
            try
            {
                //向wms读取巷道分配任务
                WMSApiDomain.WebApi.HeaderResult<WmsTaskInfoDto> res = await WCSMessageWithWMSApi.WCSCreatUploadWarehouseWMSTaskAsync(plcMessage.PalletBarcode, plcMessage.EquipmentCode.ToString());
                if (res.IsSucceed)
                {
                    workTaskDto = res.Result;
                }
                else
                {
                    _logUtil.Error($"巷道&货位请求失败：Wms请求入库任务失败，托盘号{plcMessage.PalletBarcode}，设备编号{plcMessage.EquipmentCode}！异常：{res.Message}");
                    return;
                }
            }
            catch (Exception ex)
            {
                _logUtil.Error($"巷道&货位请求失败：Wms请求入库任务失败，托盘号{plcMessage.PalletBarcode}，设备编号{plcMessage.EquipmentCode}！异常：{ex.ToString()}");
                return;
            }
            if (workTaskDto == null)
            {
                _logUtil.Info($"巷道&货位请求失败：Wms请求任务为空值，托盘号{plcMessage.PalletBarcode}，设备编号{plcMessage.EquipmentCode} ！");
                return;
            }
            else
            {

                //WorkTaskInfo数据补全
                var workTaskInfo = new WorkTaskInfo();
                long id = _snowflake.GetId();
                workTaskInfo.ID = id;
                //wcs任务code从wmstaskcode日开始截取
                int.TryParse(workTaskDto.TaskCode.ToString().Substring(6), out var wcsTaskCode);
                workTaskInfo.TaskCode = wcsTaskCode;
                //wms赋值
                workTaskInfo.WmsTaskCode = workTaskDto.TaskCode;
                workTaskInfo.DependWmsTaskCode = workTaskDto.DependWMSTaskCode;
                workTaskInfo.TaskType = Enum.Parse<EnumTaskType>(workTaskDto.TaskType.ToString());

                workTaskInfo.WmsFromPosition = workTaskDto.StartPosition;
                workTaskInfo.WmsToPosition = workTaskDto.EndPosition;
                workTaskInfo.WmsFromStation = workTaskDto.FromStation;
                workTaskInfo.WmsToStation = workTaskDto.ToStation;
                workTaskInfo.TaskProcessState = EnumTaskProcessState.Initial;
                workTaskInfo.TaskState = EnumTaskState.Executing;
                workTaskInfo.Priority = workTaskDto.Priority;

                //托盘条码
                workTaskInfo.PalletCode = plcMessage.PalletBarcode;

                #region 任务子任务信息赋值by RouteDetailInfo
                //获取配置巷道信息
                var routeInfo = await _routeInfoRepository.GetFirstAsync(s => s.FromStation == workTaskInfo.WmsFromStation
                    && s.ToStation == workTaskInfo.WmsToStation);

                if (routeInfo == null)
                {
                    _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}巷道路径信息，起始工位{workTaskInfo.WmsFromStation}目标工位{workTaskInfo.WmsToStation}获取失败");
                    return;
                }

                //巷道详情信息获取
                var routeDetailConfigInfo = await _routeDetailInfoRepository.GetFirstAsync(s => s.RouteID == routeInfo.ID && s.IsStart);

                if (routeDetailConfigInfo == null)
                {
                    _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}巷道路径详情配置信息起始工位{workTaskInfo.WmsFromStation}目标工位{workTaskInfo.WmsToStation}获取失败");
                    return;
                }

                workTaskInfo.RouteID = routeDetailConfigInfo.RouteID;
                workTaskInfo.CurrentFromCargo = routeDetailConfigInfo.FromCargo;
                workTaskInfo.CurrentToCargo = routeDetailConfigInfo.ToCargo;
                workTaskInfo.CurrentRounteDetailID = routeDetailConfigInfo.ID;
                workTaskInfo.CurrentDispatchType = routeDetailConfigInfo.DispatchType;
                //起始设备位
                workTaskInfo.CurrentFromEquipment = routeDetailConfigInfo.FromEquipment;
                workTaskInfo.CurrentToEquipment = routeDetailConfigInfo.ToEquipment;
                workTaskInfo.CurrentDetailTaskState = EnumDetailTaskState.Executing;
                workTaskInfo.CurrentCreateTime = DateTime.Now;
                workTaskInfo.CurrentUpdateTime = DateTime.Now;

                #endregion

                workTaskInfo.SaveTime = DateTime.Now;
                workTaskInfo.UpdateTime = DateTime.Now;

                #region 更新wcs任务并同步更新wms任务状态&写入正常报文
                try
                {
                    //同步更新wms任务状态为执行中
                    var flag = await WCSMessageWithWMSApi.WCSUpdateWMStaskStateAndProcessTaskStateAsync(workTaskInfo.WmsTaskCode, (int)EnumTaskState.Executing);
                    if (flag)
                    {
                        workTaskInfo.UploadWmsState = EnumUploadWmsState.Succeed;
                        workTaskInfo.UploadWmsTime = DateTime.Now;
                    }
                }
                catch (Exception ex)
                {
                    _logUtil.Error($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}wms任务号{workTaskInfo.TaskCode}同步状态更新失败{ex.ToString()}！");
                }

                //WorkTaskInfo数据插入
                bool bl = await _workTaskInfoRepository.InsertAsync(workTaskInfo);//巷道信息
                #endregion

                //如果wms请求的任务类型为工位到工位越库
                //if (workTaskDto.TaskType == (int)EnumTaskType.STS)
                //    return;

                //获取下一子任务
                var nextRouteDetailInfo = await _routeDetailInfoRepository.GetFirstAsync(s => s.PrevionsID == workTaskInfo.CurrentRounteDetailID);
                if (nextRouteDetailInfo == null)
                {
                    _logUtil.Info($"巷道&货位请求失败：设备编号{writeBo.EquipmentCode}巷道路径详情配置信息起始工位{workTaskInfo.WmsFromStation}目标工位{workTaskInfo.WmsToStation}获取失败");
                    return;
                }

                //wms请求货位
                AllotLocationOutputDto allotLocationOutputDto = null;
                if (nextRouteDetailInfo.DispatchType == EnumDispatchType.SC_UP)
                {
                    try
                    {
                        //int colum = !string.IsNullOrEmpty(routeDetailConfigInfo.FromCargo) ? Convert.ToInt32(routeDetailConfigInfo.FromCargo.Substring(4, 3)) : 1;
                        WMSApiDomain.WebApi.HeaderResult<AllotLocationOutputDto> res = await WCSMessageWithWMSApi.WCSGetAllotLocatonAsync(wcsWorkTaskInfo.WmsTaskCode, plcMessage.EquipmentCode.ToString(),
                                plcMessage.PalletBarcode, routeDetailConfigInfo.FromCargoColumn);//向WMS获取货位信息
                        if (res.IsSucceed)
                        {
                            allotLocationOutputDto = res.Result;
                        }
                        else
                        {
                            //runLogInfo.RunLog = $"向Wms请求货位失败:{res.Message}";
                            //await _IRunLogInfoService.AddRunLogInfoAsync(runLogInfo);
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        _logUtil.Error($"巷道&货位请求失败：Wms请求货位失败，托盘号{plcMessage.PalletBarcode}，设备编号{plcMessage.EquipmentCode}！异常：{ex.ToString()}");
                        return;
                    }
                }

                //装载位子任务完成 保存子任务记录
                TaskRouteDetailRecordInfo naTaskRouteDetailRecordInfo = new TaskRouteDetailRecordInfo();
                naTaskRouteDetailRecordInfo.ID = _snowflake.GetId();
                naTaskRouteDetailRecordInfo.TaskID = workTaskInfo.ID;
                naTaskRouteDetailRecordInfo.RouteDetailID = workTaskInfo.CurrentRounteDetailID;
                naTaskRouteDetailRecordInfo.DetailTaskState = EnumDetailTaskState.Finish;
                naTaskRouteDetailRecordInfo.DispatchType = workTaskInfo.CurrentDispatchType;
                naTaskRouteDetailRecordInfo.FromEquipment = workTaskInfo.CurrentFromEquipment;
                naTaskRouteDetailRecordInfo.FromCargo = workTaskInfo.CurrentFromCargo;
                naTaskRouteDetailRecordInfo.ToEquipment = workTaskInfo.CurrentToEquipment;
                naTaskRouteDetailRecordInfo.ToCargo = workTaskInfo.CurrentToCargo;
                naTaskRouteDetailRecordInfo.CreateTime = DateTime.Now;
                naTaskRouteDetailRecordInfo.UpdateTime = DateTime.Now;

                if (allotLocationOutputDto != null)
                    workTaskInfo.WmsToPosition = allotLocationOutputDto.LocationCode;
                //更新当前子任务
                workTaskInfo.CurrentRounteDetailID = nextRouteDetailInfo.ID;
                workTaskInfo.CurrentDetailTaskState = EnumDetailTaskState.Init;
                workTaskInfo.CurrentDispatchType = nextRouteDetailInfo.DispatchType;
                workTaskInfo.CurrentFromEquipment = nextRouteDetailInfo.FromEquipment;
                workTaskInfo.CurrentFromCargo = nextRouteDetailInfo.FromCargo;
                workTaskInfo.CurrentToEquipment = nextRouteDetailInfo.ToEquipment;
                workTaskInfo.CurrentToCargo = nextRouteDetailInfo.ToCargo == "ToCargo" ? allotLocationOutputDto.LocationCode : nextRouteDetailInfo.ToCargo;
                workTaskInfo.CurrentUpdateTime = DateTime.Now;
                workTaskInfo.TaskProcessState = EnumTaskProcessState.AllotLocation;

                //更新数据库
                var result2 = await _taskRouteDetailRecordInfoRepository.AsTenant().UseTranAsync(async () =>
                {
                    await _taskRouteDetailRecordInfoRepository.InsertAsync(naTaskRouteDetailRecordInfo);
                    await _workTaskInfoRepository.UpdateAsync(workTaskInfo);
                });

                //写入正常报文到Plc
                writeBo.TaskCode = workTaskInfo.TaskCode;//wcs任务号
                writeBo.ToEquipmentCode = Convert.ToInt32(workTaskInfo.CurrentToEquipment);//目标设备编号
                var writeResult = await plcConn.WriteDbAsync(writeBo.DbCode, writeBo.DbStart, writeBo.Deserialize());
                if (!writeResult)
                {
                    _logUtil.Info($"巷道&货位请求失败：PLC(IP{plcConn.IP},设备编号{writeBo.EquipmentCode})写入报文失败");
                }
                else
                {
                    _logUtil.Info($"AisleRequestService发送报文  PLCId:{writeBo.PlcConnectionID} DBId:{writeBo.PlcWriteDbID}  设备编号{writeBo.EquipmentCode} WCS握手{writeBo.ActionType}");
                }
            }
            #endregion
        }
    }
}
